
package com.lbs.core.shared.tables;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

public abstract class LbsAbstractValue implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3824198649045827301L;
    
	
	public abstract Object getValue();
	public abstract void setValue(Object v);
	
	@SuppressWarnings("unchecked")
	public static LbsAbstractValue getInstance(Object v){
		if (v instanceof String){
			return new LbsStringValue((String) v);
		}else if (v instanceof Double){
			return new LbsDoubleValue((Double) v);
		}else if (v instanceof Integer){
			return new LbsIntegerValue((Integer) v);
		}else if (v instanceof Long){
			return new LbsLongValue((Long) v);
		}else if (v instanceof AbstractData){
			return new LbsAbstractDataValue((AbstractData) v);
		}else if (v instanceof Boolean){
			return new LbsBooleanValue((Boolean) v);
		}else if (v instanceof Date){
			return new LbsDateValue((Date) v);
		}else if (v instanceof List){
			return new LbsListAbstractDataValue((List<AbstractData>) v);
		}else if (v == null){
			return new LbsNullValue();
		}else
			return new LbsObjectValue(v);
	}
	
	public static Object getInstanceFromString(String type, String v){
		if (type.equals("String")){
			return new LbsStringValue(v);
		}else if (type.equals("Double")){
			return Double.parseDouble(v);
		}else if (type.equals("Integer")){
			return Integer.parseInt(v);
		}else if (type.equals("Long")){
			return Long.parseLong(v);
		}else if (type.equals("Boolean")){
			return Boolean.parseBoolean(v);
		}else if (type == null || type.length()==0){
			return null;
		}else
			throw new RuntimeException("Tipo de dato no soportado "+ v.getClass().getName());
	}
	
	public static Object getInstanceFromStringGuessed(String v){
		try {
			Double.parseDouble(v);
			if (v.contains("."))
				return Double.parseDouble(v);
			else
				return Integer.parseInt(v);
		}catch (Exception e){
			if (v.equals("true") || v.equals("false"))
				return Boolean.parseBoolean(v);
			else
				return v.toString();
		}
	}
	
	@Override
	public String toString() {
		return getValue().toString();
	}
	
	
}
